/**
 * 
 */
package com.jszw.bcp.kernal.page;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jszw.bcp.kernal.log.TraceLog;

/**
 * @author zhengzw
 *
 */
@Controller
public class ForwardController {
	
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String login() {
		return "index";
	}
	
	@TraceLog(value = "平台页面跳转")
	@RequestMapping(value = "/**/{path}.xhtml", method = RequestMethod.GET)
	public String forwardPage(HttpServletRequest request, Map<String, Object> map) {
		handlerParameters(request, map);
		String page = handlerForwardPage(request);
		return page;
	}
	
	/**
	 * 处理请求参数
	 * @param request
	 * @param map
	 */
	private void handlerParameters(HttpServletRequest request, Map<String, Object> map) {
		Enumeration<String> names = request.getParameterNames();
		while(names.hasMoreElements()) {
			String name = names.nextElement();
			String value = request.getParameter(name);
			logger.info("发送请求参数-->参数名:[{}] 参数值 :[{}]", name, value);
			map.put(name, value);
		}
	}
	
	/**
	 * 处理页面跳转url
	 * @param request
	 * @return
	 */
	private String handlerForwardPage(HttpServletRequest request) {
		String url = request.getRequestURI();
		url = url.replaceAll(request.getContextPath(), "");
		url = url.replaceAll(".xhtml", "");
		
		logger.info("跳转页面路径-->地址:[{}]", url);
		
		return url;
	}
}